import {Http} from "./http";
class Paging {
    start;
    count;
    baseUrl;
    requestStatus;
    requestParam;
    is_end;
    currentData=[];

    constructor(reqUrl, param = {}, start = 0, count = 10) {
        this.start = start;
        this.count = count;
        this.baseUrl = reqUrl;
        this.requestParam = param;
        this.is_end = false;

    }

    async getData() {
        if (this.requestStatus) {
            return this.__getError();
        }
        if (this.is_end) {
            return this.__getError();
        }
        let reqParam = this.__getRequestParam();
        let data = await Http.request(this.baseUrl, reqParam);

        this.__managerData(data);
        this.__setRequestStatus();
        return this.__getSuccess();
    }

    __getSuccess() {
        return {
            'code': true,
            "data": this.currentData
        }
    }

     __managerData(requestReturnData) {
        if (!requestReturnData.data.items) {
            this.is_end = true;
            return null;
        }
        this.currentData = this.currentData.concat(requestReturnData.data.items);
    }

    __getRequestParam() {
        let newParam = {
            "start": this.start,
            "count": this.count
        };
        //this.count=this.count+this.start+1;
        this.start = this.count + this.start;
        return Object.assign(newParam, this.requestParam);
    }

    __setRequestStatus() {
        this.requestStatus = !this.requestStatus;
    }

    __getError() {
        return {
            'code': false
        }
    }

}
export {
    Paging
}